package com.shemy.sshiro.controller;

import com.shemy.sshiro.domain.SysUser;
import com.shemy.sshiro.service.SysUserService;
import com.shemy.sshiro.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author Dzsom
 * @Date 2019-05-13 19:41
 * @Encoding UTF-8
 * @Version 1.0
 **/
@RestController
@RequestMapping("/login")
public class LoginController {

    @PostMapping
    public Result login(@RequestBody SysUser user){
        UsernamePasswordToken token = new UsernamePasswordToken(user.getAccount(), user.getPassword(), "login");
        Subject subject = SecurityUtils.getSubject();
        token.setRememberMe(true);
        try {
            subject.login(token);
        }catch (IncorrectCredentialsException e){
            return new Result(false,2001,"登录失败！");
        }

        if (subject.isAuthenticated()){
            Map<String,Object> map=new HashMap<>(16);
            map.put("nickName",user.getNickname());
            map.put("userName",user.getAccount());
            map.put("token",token);
            return new Result(true,2000,"登陆成功！",map);
        }
        return new Result(false,2001,"登录失败！");
    }

    @GetMapping
    public Result add(){
        return new Result(false,2001,"登录失败！");
    }

}
